package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.PermissionService;
import cn.wolfcode.wms.util.RequiredPermission;
import cn.wolfcode.wms.vo.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;
    @RequiredPermission("权限查询")
    @RequestMapping("/list")
    public String list(Map map, QueryObject qo) {
        PageResult result = permissionService.query(qo);
        map.put("result", result);
        return "permission/list";
    }

    @RequiredPermission("权限删除")
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id) {

        try {
            permissionService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark(e.getMessage());
        }
        return JSONResult.success("删除成功");
    }

    @RequiredPermission("权限加载")
    @RequestMapping("/load")
    @ResponseBody
    public JSONResult load(Long id) {
        try {
            permissionService.load();
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark(e.getMessage());
        }
        return JSONResult.success("加载成功");
    }

}
